<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_Entry extends Controller_Common
{
    public function action_entry()
    {
        $users = ORM::factory('user')
            ->where('login', '=', Cookie::get('login','no user'))
            ->find();
        if(Cookie::get('login', 'no user')==$users->login && Cookie::get('pas')==$users->password )
        {
            if($users->accesses==1)
            {
                $this->redirect('admin/adm');
            }
            else
            {
                if($users->confirm==1)
                {
                    $this->redirect('user/us');
                }
                else
                {
                    $this->redirect('entry/login');
                }
            }
        }
        else
        {
            $this->redirect('entry/login');
        }
    }
    public function action_login()
    {
         if(isset($_POST['confirm']))
        {

                    $login=Arr::get($_POST, 'login', '');
            $pass=Arr::get($_POST, 'password', '');
            $users = ORM::factory('user')
             ->where('login', '=', $login)
             ->find();
         
         if($users->loaded())
       {  
        if($login==$users->login && $pass==$users->password)
        {
          if($users->accesses==1)
          {
              Cookie::set('login', $_POST['login']);
              Cookie::set('pas', $_POST['password']);
              Cookie::set('id', $users->id);
              $this->redirect('admin/adm');
          }
            else
            {
                if($users->confirm==1)
                {
                    Cookie::set('login', $_POST['login']);
                    Cookie::set('pas', $_POST['password']);
                    Cookie::set('id', $users->id);
                    $this->redirect('user/us');
                }
                else
                {
                   $error = View::factory('errors/error1');
                    $this->template->error = $error;                }
            }
        }
             else
             {
                    $error = View::factory('errors/error2');
                    $this->template->error = $error;   
             }
         }
         else
         {
               $error = View::factory('errors/error3');
                    $this->template->error = $error;   
         }
         }
     $content = View::factory('login');
     $this->template->content = $content; 
 }
    
    
}